import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { createTransport, Transporter } from 'nodemailer';

@Injectable()
export class EmailService {
  transporter: Transporter
  constructor(private configService: ConfigService){
    this.transporter = createTransport({
      host: this.configService.get('nodemailer_host'),
      port: this.configService.get('nodemailer_port'),
      // port: 465,
      // secure: true, //使用 SSL 就用465端口，否则用 587
      secure: false,
      auth: {
        user: this.configService.get('nodemailer_auth_user'),
        pass: this.configService.get('nodemailer_auth_pass')
      }
    })
  }

  async sendMail({to, subject,html}){
     await this.transporter.sendMail({
      from:{
        name:'会议室预定系统',
        address: this.configService.get('nodemailer_auth_user')
      },
      to,
      subject,
      html
    })
  }
}
